package postjnlv2_param.modele;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

public class ReadFile {
	
	private String path="";
	private GetDesktopPath getDesktopPath;
	
	public ReadFile(){
		getDesktopPath=new GetDesktopPath();
		path=getDesktopPath.getApplicationPath();
	}
	
	
	//INFO MAX(JNL_END_DT)
	public String getMAX_JNL_END_DT_Request(){	
		File fileIn=new File(path+"requetePostJNL/Max(jnl_end_dt).sql");		
		String chaine="";
		//lecture du fichier texte	
		try{
			InputStream ips=new FileInputStream(fileIn); 
			InputStreamReader ipsr=new InputStreamReader(ips);
			BufferedReader br=new BufferedReader(ipsr);
			String ligne;
			while ((ligne=br.readLine())!=null){
				chaine+=ligne+" \n";
			}
			br.close(); 
		}		
		catch (Exception e){
			System.out.println(e.toString());
		}
		
		chaine=new String(chaine.getBytes(),Charset.forName("UTF-8"));
		return chaine;
	}
	
	//INFO MAX(JNL_REF_NO)
	public String getMAX_JNL_REF_NO_Request(){		
		
		File fileIn=new File(path+"requetePostJNL/Max(jnl_ref_no).sql");
		String chaine="";
		//lecture du fichier texte	
		try{
			InputStream ips=new FileInputStream(fileIn); 
			InputStreamReader ipsr=new InputStreamReader(ips);
			BufferedReader br=new BufferedReader(ipsr);
			String ligne;
			while ((ligne=br.readLine())!=null){
				chaine+=ligne+" \n";
			}
			br.close(); 
		}		
		catch (Exception e){
			System.out.println(e.toString());
		}
		chaine=new String(chaine.getBytes(),Charset.forName("UTF-8"));
		return chaine;
	}
	
	//INFO T_ID_RUN FROM CPTL_TRT01_POSTJNLV2_ARCH 
	public String getT_ID_RUN_Request(String jnlRefNo1,String jnlRefNo2){
		
		File fileIn=new File(path+"requetePostJNL/Info_id_run.sql");
		String chaine="";
		//lecture du fichier texte	
		try{
			InputStream ips=new FileInputStream(fileIn); 
			InputStreamReader ipsr=new InputStreamReader(ips);
			BufferedReader br=new BufferedReader(ipsr);
			String ligne;
			while ((ligne=br.readLine())!=null){
				chaine+=ligne+" \n";
			}
			br.close(); 
		}		
		catch (Exception e){
			System.out.println(e.toString());
		}
		chaine=chaine.replaceAll("jnlRefNo1", jnlRefNo1);
		chaine=chaine.replaceAll("jnlRefNo2", jnlRefNo2);
		chaine=new String(chaine.getBytes(),Charset.forName("UTF-8"));
		return chaine;
	}
	
	//UPDATE CPTL_TRT01_POSTJNLV2_ARCH
	public String getUpdate_CPTL_TRT01_POSTJNL_Request(String idRun,String date1,String date2){	
		
		File fileIn=new File(path+"requetePostJNL/Update_CPTL_TRT01_POSTJNLV2_ARCH.sql");
		String chaine="";
		//lecture du fichier texte	
		try{
			InputStream ips=new FileInputStream(fileIn); 
			InputStreamReader ipsr=new InputStreamReader(ips);
			BufferedReader br=new BufferedReader(ipsr);
			String ligne;
			while ((ligne=br.readLine())!=null){
				chaine+=ligne+" \n";
			}
			br.close(); 
		}		
		catch (Exception e){
			System.out.println(e.toString());
		}
		chaine=chaine.replaceAll("idRun", idRun);
		chaine=chaine.replaceAll("date1", date1);
		chaine=chaine.replaceAll("date2", date2);
		chaine=new String(chaine.getBytes(),Charset.forName("UTF-8"));
		return chaine;
	}
	
	//UPDATE JNL_RUNS_STATUS
	public String getUpdate_JNL_RUNS_STATUS_Request(String date1,String jnlRefNo1,String jnlRefNo2){
		
		File fileIn=new File(path+"requetePostJNL/Update_JNL_RUN_STATUS.sql");
		String chaine="";
		//lecture du fichier texte	
		try{
			InputStream ips=new FileInputStream(fileIn); 
			InputStreamReader ipsr=new InputStreamReader(ips);
			BufferedReader br=new BufferedReader(ipsr);
			String ligne;
			while ((ligne=br.readLine())!=null){
				chaine+=ligne+" \n";
			}
			br.close(); 
		}		
		catch (Exception e){
			System.out.println(e.toString());
		}
		chaine=chaine.replaceAll("date1", date1);
		chaine=chaine.replaceAll("jnlRefNo1", jnlRefNo1);
		chaine=chaine.replaceAll("jnlRefNo2", jnlRefNo2);
		chaine=new String(chaine.getBytes(),Charset.forName("UTF-8"));
		return chaine;
	}
	
	//UPDATE JNL_RUNS_STATUS
	public String getReplace_Cptl_Trt01_Postjnlv2_Pkg_Request(String idRun){
		
		File fileIn=new File(path+"requetePostJNL/Cptl_Trt01_Postjnlv2_Pkg.sql");
//		File fileIn=new File(path+"requetePostJNL/cptl_test_toto_pkg.sql");
		String chaine="";
		//lecture du fichier texte	
		try{
			InputStream ips=new FileInputStream(fileIn); 
			InputStreamReader ipsr=new InputStreamReader(ips);
			BufferedReader br=new BufferedReader(ipsr);
			String ligne;
			while ((ligne=br.readLine())!=null){
				chaine+=ligne+" \n";
			}
			br.close(); 
		}		
		catch (Exception e){
			System.out.println(e.toString());
		}
		chaine=chaine.replaceAll("idRun", idRun);
		chaine=new String(chaine.getBytes(),Charset.forName("UTF-8"));
		return chaine;
	}
	
	
	
}